<?php

class LearnPdocAction extends CommonAction {


    public function index(){

        $sn = I('sn','SN_0');

        $pdoc = M('Pdoc')->where(array('pdoc_sn'=>$sn))->find();

        if(!$pdoc){
            $this->error("文章不存在！");
            exit();
        }
        
        $uid = $_SESSION['current_member']['uid'];
        $time_study = M('MMembersPdoc')->where(array('uid'=>$uid, 'pdoc_id'=>$pdoc['pdoc_id']))->getField('time_study');
        
        
        if( $_SESSION['current_member']['study_status'] != 0 ){
            $this->error("已存在正在学习的文章...",U('closePage'));
            exit();
        }
        
        $_SESSION['current_member']['study_status'] = 1;
        $_SESSION['current_member']['cur_time'] = date( "y-m-d H:i:s", time());

        $names = getDocFileNames(UPLOAD_PATH.$pdoc['pdoc_sn']);

        //重新排序一下
        usort($names, "strnatcmp");

        for ($i=0; $i < count($names); $i++) { 
            $pdoc['imgs'][$i] = $pdoc['pdoc_sn'].'/'.$names[$i];
        }

        $this->assign("pdoc", $pdoc);
        $this->assign("imgs", $pdoc['imgs']);
        $this->assign("time_study",$time_study);
        $this->display();

    }
    
    public function closePage(){
        echo "<script type='text/javascript'>document.onload = window.close();</script>";
    }
    
    public function saveTimeStudy()
    {
        # code...
        $_SESSION['current_member']['study_status'] = 0;
        $db_m_members_pdoc = M('MMembersPdoc');
        
        $db_m_members_pdoc->create();
        $db_m_members_pdoc->start_time = $_SESSION['current_member']['cur_time'];
        $db_m_members_pdoc->stop_time = date( "y-m-d H:i:s", time());
        if($db_m_members_pdoc->add()){
            $this->ajaxReturn(array('status' => 1, 'info' => '保存成功！', 'data' => null, 'jumpUrl' => U('PdocList/index') ));
        }else{
            $this->ajaxReturn(array('status' => 0, 'info' => '保存失败', 'data' => null, 'jumpUrl' => U('PdocList/index') ));
        }

    }

    public function saveTimeStudy_bak()
    {
        # code...
        $_SESSION['current_member']['study_status'] = 0;
        $db_m_members_pdoc = M('MMembersPdoc');
        
        $uid = I('uid', 0);
        $pdoc_id = I('pdoc_id', 0);
        
        $res = $db_m_members_pdoc->where(array('uid'=>$uid,'pdoc_id'=>$pdoc_id))->find();
        
        if($res){
            $db_m_members_pdoc->create();
            $db_m_members_pdoc->last_date = date( "y-m-d H:i:s", time());
            $db_m_members_pdoc->id = $res['id'];
            if($db_m_members_pdoc->save()){
                $this->ajaxReturn(array('status' => 1, 'info' => '保存成功！', 'data' => null, 'jumpUrl' => U('PdocList/index') ));
            }else{
                $this->ajaxReturn(array('status' => 0, 'info' => '保存失败', 'data' => null, 'jumpUrl' => U('PdocList/index') ));
            }
        }
        else{
            $db_m_members_pdoc->create();
            $db_m_members_pdoc->create_date = date( "y-m-d H:i:s", time());
            $db_m_members_pdoc->last_date = date( "y-m-d H:i:s", time());
            if($db_m_members_pdoc->add()){
                $this->ajaxReturn(array('status' => 1, 'info' => '保存成功！', 'data' => null, 'jumpUrl' => U('PdocList/index') ));
            }else{
                $this->ajaxReturn(array('status' => 0, 'info' => '保存失败', 'data' => null, 'jumpUrl' => U('PdocList/index') ));
            }
            
        }
        
        

    }
    
    public function clearSession(){
        $_SESSION['current_member']['study_status'] = 0;
    }


}